<template>
  <div class="page-container">
    <div class="page-content">
      <!--搜索-->
      <div class="panel-container">
        <searchuinew :searchFormArr="searchFormArr" @searchParma="searchParma" :isAccording="isAccording"></searchuinew>
      </div>
      <!--数据信息-->
      <div class="panel-container no-margin">
        <div class="panel-line">
          <p class="text-1">
            合同模板下载
            <a :href="url" download>
              <span class="link">《Feijiu网资产处置合作服务协议.doc》</span>
            </a>
          </p>
          <el-button size="mini" type="primary" @click="handleSign">+ 线下签约</el-button>
        </div>
        <el-table :data="dataList" size="mini" :header-cell-style="{ background: '#F5F5F5', color: '#000000' }" ref="table" style="width: 100%" v-loading="dataListLoading">
          <el-table-column align="center" header-align="center" label="序号" type="index" width="50"></el-table-column>
          <el-table-column align="center" header-align="center" label="企业名称" prop="com_name" width="200"></el-table-column>
          <el-table-column align="center" header-align="center" label="联系人" prop="user_name"></el-table-column>
          <el-table-column align="center" header-align="center" label="联系电话" prop="user_phone"></el-table-column>
          <el-table-column align="center" header-align="center" label="是否添加微信" prop="is_add_wx_show" width="150"></el-table-column>
          <el-table-column align="center" header-align="center" label="签约方式" prop="sign_method_show" width="150"></el-table-column>
          <el-table-column align="center" header-align="center" label="跟进人" prop="follow_name"></el-table-column>
          <el-table-column align="center" header-align="center" label="签约时间" prop="sign_time"></el-table-column>
          <el-table-column align="center" fixed="right" header-align="center" label="操作" width="200">
            <template slot-scope="scope">
              <el-button size="mini" type="text" @click="handleView(scope)">查看</el-button>
              <el-button size="mini" type="text" @click="handleEdit(scope)">修改</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          small
          background
          :current-page="pageIndex"
          :page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="totalPage"
          @current-change="currentChangeHandle"
          @size-change="sizeChangeHandle"
          layout="total, sizes, prev, pager, next, jumper"
          style="margin-top: 20px; text-align: right"
        ></el-pagination>
      </div>
    </div>

    <!--  线下签约弹窗-->
    <el-drawer title="线下签约" :visible.sync="showOfflineSign" :destroy-on-close="true" size="40%">
      <OfflineSign :id="currentId" :mode="editMode" @close="showOfflineSign = false" @refresh="handleRefresh" />
    </el-drawer>

    <!--  线下签约查看弹窗-->
    <el-drawer title="查看" :visible.sync="showOfflineSignView" :destroy-on-close="true" size="40%">
      <OfflineSignView :id="currentId" :mode="editMode" @close="showOfflineSignView = false" @refresh="handleRefresh" />
    </el-drawer>
  </div>
</template>

<script>
import { apiSignUserList, apiApplyCustom } from '@/api/assetsDisposal'
import OfflineSign from './components/OfflineSign'
import OfflineSignView from './components/OfflineSignView'
export default {
  name: 'index',
  components: { OfflineSign, OfflineSignView },
  data() {
    return {
      searchFormArr: [
        {
          fieldName: '企业名称：',
          type: 'input',
          field: 'com_name',
          placeholder: '请输入企业名称',
        },
        {
          fieldName: '联系人：',
          type: 'input',
          field: 'name',
          placeholder: '请输入联系人',
        },
        {
          fieldName: '联系电话：',
          type: 'input',
          field: 'phone',
          placeholder: '请输入联系电话',
        },
        {
          fieldName: '是否添加微信：',
          type: 'select',
          field: 'is_add_wx',
          option: [
            { id: 1, text: '否' },
            { id: 2, text: '是' },
          ],
          placeholder: '请选择',
        },
        {
          fieldName: '签约方式：',
          type: 'select',
          field: 'sign_method',
          option: [
            { id: 1, text: '线上' },
            { id: 2, text: '线下' },
          ],
          placeholder: '请选择',
        },
        {
          fieldName: '跟进人：',
          type: 'input',
          field: 'follow_name',
          placeholder: '请输入跟进人',
        },
        {
          fieldName: '签约时间',
          type: 'timePicker',
          field: 'time',
          placeholder: '请选择签约时间',
          timeType: 'daterange',
          format: 'yyyy-MM-dd',
        },
      ],
      isAccording: true,
      searchParams: {
        com_name: '',
        name: '',
        phone: '',
        is_add_wx: '',
        sign_method: '',
        follow_name: '',
        start_time: '',
        end_time: '',
      },

      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      editMode: 'add',
      currentId: '',
      showOfflineSign: false,
      showOfflineSignView: false,
      url: '', // 合同地址
    }
  },
  mounted() {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      const info = Object.assign({ pageNumber: this.pageIndex, pageSize: this.pageSize }, this.searchParams)
      const formData = new FormData()
      for (let key in info) {
        formData.set(key, info[key])
      }
      apiSignUserList(formData)
        .then((res) => {
          const { items, total, url } = res.result
          items.forEach((item) => {
            item.is_add_wx_show = parseInt(item.is_add_wx) === 1 ? '否' : '是'
            item.sign_method_show = parseInt(item.sign_method) === 1 ? '线上' : parseInt(item.sign_method) === 2 ? '线下' : '待签约'
          })
          this.dataList = items
          this.totalPage = total
          this.url = url
          this.dataListLoading = false
        })
        .catch((e) => {
          this.dataList = []
          this.totalPage = 0
          this.dataListLoading = false
        })
    },
    // 列表查询
    searchParma(e) {
      this.searchFormArr.forEach((item) => {
        if (item.field === 'time') return
        if (e[item.field]) {
          this.searchParams[item.field] = e[item.field]
        } else {
          this.searchParams[item.field] = ''
        }
      })
      if (e.time) {
        this.searchParams.start_time = e.time[0]
        this.searchParams.end_time = e.time[1]
      } else {
        this.searchParams.start_time = this.searchParams.end_time = ''
      }
      this.pageIndex = 1
      this.getDataList()
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 查看
    handleView(scope) {
      this.showOfflineSignView = true
      this.currentId = scope.row.id
    },
    // 修改
    handleEdit(scope) {
      this.editMode = 'edit'
      this.showOfflineSign = true
      this.currentId = scope.row.id
    },
    // 线下签约
    handleSign() {
      this.editMode = 'add'
      this.showOfflineSign = true
    },
    // 刷新列表
    handleRefresh() {
      this.showOfflineSign = false
      this.getDataList()
    },
  },
}
</script>

<style scoped lang="scss">
.panel-line {
  margin-bottom: 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.text-1 {
  .link {
    color: #409eff;
    cursor: pointer;
  }
}
</style>
